package com.ac.scum.log.web.model.player_score.controller;

import com.ac.common.core.exception.BaseException;
import com.ac.common.core.result.Result;
import com.ac.scum.log.web.model.player.service.PlayerService;
import com.ac.scum.log.web.model.player_score.service.PlayerScoreUnbombService;
import com.ac.scum.log.web.model.player_score.vo.ScoreRequestVO;
import com.ac.scum.log.web.utils.user.CurrentUserUtil;
import com.ac.scum.player.score.bean.PlayerScoreUnbomb;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("player_unbomb_score")
public class PlayerUnbombScoreController {
    private final PlayerService playerService;
    private final PlayerScoreUnbombService playerScoreUnbombService;

    @PostMapping("page/{current}/{size}")
    public Result<Page<PlayerScoreUnbomb>> page(@PathVariable("current") Long current, @PathVariable("size") Long size,  @RequestBody(required = false) ScoreRequestVO scoreRequestVO) throws BaseException {
        LambdaQueryWrapper<PlayerScoreUnbomb> where = Wrappers.<PlayerScoreUnbomb>lambdaQuery()
                .eq(PlayerScoreUnbomb::getServerId, CurrentUserUtil.getCurrentServerId())
                .orderByDesc(PlayerScoreUnbomb::getCountDate, PlayerScoreUnbomb::getAvgSuccessNum);
        if (scoreRequestVO!=null) {
            if (scoreRequestVO.getSteamId() != null) {
                where.eq(PlayerScoreUnbomb::getSteamId, scoreRequestVO.getSteamId());
            }
            if (scoreRequestVO.getStartTime() != null) {
                where.gt(PlayerScoreUnbomb::getCountDate, scoreRequestVO.getStartTime());
            }
            if (scoreRequestVO.getEndTime() != null) {
                where.lt(PlayerScoreUnbomb::getCountDate, scoreRequestVO.getEndTime());
            }
        }

        Page<PlayerScoreUnbomb> page = playerScoreUnbombService.page(new Page<>(current, size), where);
        for (PlayerScoreUnbomb record : page.getRecords()) {
            record.setPlayer(playerService.findPlayerById(record.getServerId(), record.getPlayerId()));
        }
        return Result.ok(page);
    }
}
